package com.lineage.server.timecontroller.server;

import com.lineage.server.model.Instance.L1TrapInstance;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.WorldTrap;
import java.util.HashMap;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServerTrapTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(ServerTrapTimer.class);
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 5000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 5000L, 5000L);
  }
  
  public void run() {
    try {
      HashMap allTrap = WorldTrap.get().map();
      if (allTrap.isEmpty())
        return; 
      Object[] array;
      int length = (array = allTrap.values().toArray()).length;
      int i = 0;
      while (i < length) {
        Object iter = array[i];
        L1TrapInstance trap = (L1TrapInstance)iter;
        if (!trap.isEnable()) {
          trap.set_stop(trap.get_stop() + 1);
          if (trap.get_stop() >= trap.getSpan()) {
            trap.resetLocation();
            Thread.sleep(50L);
          } 
        } 
        i++;
      } 
    } catch (Exception io) {
      _log.error("陷阱召唤处理时间轴异常重启", io);
      GeneralThreadPool.get().cancel(this._timer, false);
      ServerTrapTimer trapTimer = new ServerTrapTimer();
      trapTimer.start();
    } 
  }
}
